package org.kfive.nova.test;


import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/3/16.
 */
public class Mocker<T extends Exception> {
    private void pleaseThrow(final Exception t) throws T{
        throw (T)t;
    }

    public static void main(String[] args) {
        /*Map<String,Object> collection = new TreeMap<>();
        System.out.println(collection.compute("foo",(k,v)->(v==null)?new ArrayList<>():((List)v).add("bar")));
        System.out.println(collection.compute("foo",(k,v)->(v==null)?new ArrayList<>():((List)v).add("ber")));*/
        /*int [] params = new int[]{3,2,4};
        int [] result = twoSum(params,6);
        System.out.println(result[0]+":"+result[1]);*/
//        System.out.println(lengthOfLongestSubstring("bbbbb"));
        System.out.println(findMedianSortedArrays(new int[]{1,2},new int[]{3,4}));
    }

    public static void call(Mocker... params){
        System.out.println(params[0]);
        System.out.println(params[1]);
        params[0] = params[1];
        System.out.println(params[0]);
        System.out.println(params[1]);
    }

    public static int[] twoSum(int[] nums, int target) {
        int result[] = new int[2];
        Map<Integer,Integer> map = new HashMap<Integer,Integer>();
        for(int i=0;i<nums.length;i++){
            int tmp = target-nums[i];
            if(map.containsKey(nums[i])){
                result[0] = map.get(nums[i]);
                result[1] = i;
                break;
            }
            map.put(tmp,i);
        }
        return result;
    }

    public static int lengthOfLongestSubstring(String s) {
        if(s==null||s.length()<=0 ){
            return 0;
        }
        int maxLength=0;
        int subStart = 0;
        int length=0;
        int start = 0;
        Map<Character,Integer> map = new HashMap<Character,Integer>();
        char[] chars = s.toCharArray();
        for(int i=0;i<chars.length;i++){
            length++;
            if(map.containsKey(chars[i])){
                start = map.get(chars[i]);
                if(start>=subStart){
                    start = start+1;
                    length = length - start + subStart;
                    subStart = start;
                }
            }
            map.put(chars[i],i);
            if(length>maxLength){
                maxLength = length;
            }
        }
        return maxLength;
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        Double median = 0d;
        int size = 0;
        if(nums1!=null&&nums1.length>0){
            size += nums1.length;
        }
        if(nums2!=null && nums2.length>0){
            size += nums2.length;
        }
        int []combine = new int[size];
        int index1 = 0;
        int index2 = 0;
        int index = 0;
        while(index1<nums1.length&&index2<nums2.length){
            if(nums1[index1]<nums2[index2]){
                combine[index++] = nums1[index1++];
            }else{
                combine[index++] = nums2[index2++];
            }
        }
        if(index1>=index2){
            while(index2<nums2.length){
                combine[index++] = nums2[index2++];
            }
        }
        if(index1<=index2){
            while(index1<nums1.length){
                combine[index++] = nums1[index1++];
            }
        }
        boolean isEven = (size%2==0);
        if(isEven){
            median = (combine[size/2-1]+combine[size/2])/2.0;
        }else{
            median = combine[(size+1)/2-1]/1.0;
        }
        return median;
    }
}
